<template>
  <el-dialog
    v-model="dialogVisible"
    title="辅助工上机"
    width="1000px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="basePersonQuery">
      <el-row>
        <el-col :span="8">
          <el-form-item label="编码">
            <el-input
              v-model="basePersonQuery.personCode"
              placeholder="人员编码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="名称">
            <el-input
              v-model="basePersonQuery.personName"
              placeholder="人员名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" @click="queryPerson">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-divider />
    <el-table
      :data="personList"
      :highlight-current-row="true"
      size="small"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="45" />
      <el-table-column label="人员编码" prop="personCode" />
      <el-table-column label="人员名称" prop="personName" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="comfirmShangGong">上工</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { getBasePersonPageApi } from '@/api/base/person/index'
  import { shangFuzhuUserBatchApi } from '@/api/equip/equip_api'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const dialogVisible = ref(false)

  const basePersonQuery = ref({})
  const personList = ref()
  const selectedRows = ref()
  const equipData = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const openDialog = async (value) => {
    dialogVisible.value = true
    equipData.value = value
    queryPerson()
  }

  const queryPerson = async () => {
    const load = $baseLoading()
    try {
      let params = { ...paginationParams, ...basePersonQuery.value }
      const { code, msg, data } = await getBasePersonPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        personList.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const comfirmShangGong = async () => {
    const load = $baseLoading()
    try {
      let params = []
      selectedRows.value.forEach((element) => {
        let map = {
          pkEqlist: equipData.value.pkEqlist,
          fuUserName: element.personName,
          userId: element.personCode,
          banciName: equipData.value.banciName,
          day: equipData.value.day,
          month: equipData.value.month,
          banciDate: equipData.value.banciDate,
        }
        params.push(map)
      })
      const { code, msg } = await shangFuzhuUserBatchApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const handleSelectionChange = (rows) => (selectedRows.value = rows)

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
    queryEquip()
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 10
    paginationParams.pageNum = 1
    queryEquip()
  }
  //#endregion pagintion 结束

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
